package com.yunshipei.model;

import android.os.Environment;
import android.os.StatFs;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by cike on 15-11-4.
 */
public class SDcardInfo {
    StatFs stat;
    long blockSize, totalSize, availableBlocks;
    public SDcardInfo() {
        //SD卡路径
        File filePath = Environment.getExternalStorageDirectory();
        stat = new StatFs(filePath.getPath());
        blockSize = stat.getBlockSize();   //获得扇区的大小
        totalSize = stat.getBlockCount();  //获取的数量
        availableBlocks = stat.getAvailableBlocks();   //获取可用扇区的数量
    }

    /**
     * 获取文件目录总容量
     * @return
     */
    public long getTotalMemory(){
        long totalMemory;
        totalMemory = totalSize * blockSize;
        return totalMemory;
    }

    /**
     * 获取已用容量
     * @return
     */
    public long getUsedMemory() {
        return getTotalMemory() - getAvailableMemory();
    }

    /**
     * 获取文件目录可用容量
     * @return
     */
    public long getAvailableMemory(){
        long availableMemory;
        availableMemory = blockSize * availableBlocks;
        return availableMemory;
    }

    /**
     * 判断sdcard是否存在
     * @return
     */
    public static boolean sdCardExist() {
        boolean sdCardExist = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        return sdCardExist;
    }

    private ArrayList<String> getDevMountList() {
        File file = new File("/etc/vold.fstab");
        String[] toSearch;
        ArrayList<String> out = new ArrayList<>();
        try {
            toSearch = FileUtils.readFileToString(file).split(" ");
        for (int i = 0;i < toSearch.length; i++) {
            if (toSearch[i].contains("dev mount")) {
                if (new File(toSearch[i + 2]).exists()) {
                    out.add(toSearch[i + 2]);
                }
            }
        }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out;
    }

    public String getExternalPath() {
        String path = null;
        File sdCardFile = null;
        ArrayList<String> devMountList = getDevMountList();
        for (String devMount : devMountList) {
            File file = new File(devMount);
            if (file.isDirectory() && file.canWrite()) {
                path = file.getAbsolutePath();
                String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
                File testWritable = new File(path, "test_" + timeStamp);
                if (testWritable.mkdirs()) {
                    testWritable.delete();
                } else {
                    path = null;
                }
            }
        }
        if (path != null) {
            System.out.println(path);
            sdCardFile = new File(path);
            System.out.println(sdCardFile.getAbsoluteFile());
            return path;
        }
        return null;
    }


}
